import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WriteRunner {
    public String readTxt(){//读文本
        try {

            InputStreamReader
                    isr =
                    new InputStreamReader
                            (new FileInputStream(
                                    new File(
                                            "ESSAY.txt")));
            BufferedReader bufferedReader =new BufferedReader(isr);
            String str;
            String txt="";
            while ((str=bufferedReader.readLine())!=null){
                txt+=str;
            }
            System.out.println(txt);

            bufferedReader.close();
            isr.close();
            return txt;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Integer keyEMap(char word){
        switch (word) {
            case 'a':
               return KeyEvent.VK_A;
            case 'b':
               return KeyEvent.VK_B;
            case 'c':
               return KeyEvent.VK_C;
            case 'd':
               return KeyEvent.VK_D;
            case 'e':
               return KeyEvent.VK_E;
            case 'f':
               return KeyEvent.VK_F;
            case 'g':
               return KeyEvent.VK_G;
            case 'h':
               return KeyEvent.VK_H;
            case 'i':
               return KeyEvent.VK_I;
            case 'j':
               return KeyEvent.VK_J;
            case 'k':
               return KeyEvent.VK_K;
            case 'l':
               return KeyEvent.VK_L;
            case 'm':
               return KeyEvent.VK_M;
            case 'n':
               return KeyEvent.VK_N;
            case 'o':
               return KeyEvent.VK_O;
            case 'p':
               return KeyEvent.VK_P;
            case 'q':
               return KeyEvent.VK_Q;
            case 'r':
               return KeyEvent.VK_R;
            case 's':
               return KeyEvent.VK_S;
            case 't':
               return KeyEvent.VK_T;
            case 'u':
               return KeyEvent.VK_U;
            case 'v':
               return KeyEvent.VK_V;
            case 'w':
               return KeyEvent.VK_W;
            case 'x':
               return KeyEvent.VK_X;
            case 'y':
               return KeyEvent.VK_Y;
            case 'z':
               return KeyEvent.VK_Z;

            case ',':
               return KeyEvent.VK_COMMA;
            case '.':
               return KeyEvent.VK_PERIOD;
            case ';':
                return KeyEvent.VK_SEMICOLON;
            case '_':
                return KeyEvent.VK_UNDERSCORE;
            case '!':
                return KeyEvent.VK_EXCLAMATION_MARK;
            case '/':
                return KeyEvent.VK_SLASH;
            case '0':
                return KeyEvent.VK_0;
            case '1':
                return KeyEvent.VK_1;
            case '2':
                return KeyEvent.VK_2;
            case '3':
                return KeyEvent.VK_3;
            case '4':
                return KeyEvent.VK_4;
            case '5':
                return KeyEvent.VK_5;
            case '6':
                return KeyEvent.VK_6;
            case '7':
                return KeyEvent.VK_7;
            case '8':
                return KeyEvent.VK_8;
            case '9':
                return KeyEvent.VK_9;

        }
        return KeyEvent.VK_SPACE;
    }

    public void write(){//模拟输出
        try {
            String txt = readTxt();
            Robot robot =new Robot();
            robot.delay(5000);//5秒缓充时间
            for(int i=0;i<txt.length();i++){
                char word = txt.charAt(i);
                //判断是大写还是小写
                boolean up = Character.isUpperCase(word);
                //如果大写调用两下下VK_CAPS_LOCK
                if (up){
                    robot.keyPress(KeyEvent.VK_CAPS_LOCK);
                    robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
                    robot.keyPress(keyEMap(Character.toLowerCase(word)));
                    robot.keyRelease(keyEMap(Character.toLowerCase(word)));
                    robot.keyPress(KeyEvent.VK_CAPS_LOCK);
                    robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
                }else {
                    if(word=='?'){
                        robot.keyPress(KeyEvent.VK_SHIFT);
                        robot.keyPress(KeyEvent.VK_SLASH);
                        robot.keyRelease(KeyEvent.VK_SLASH);
                        robot.keyRelease(KeyEvent.VK_SHIFT);
                    }
                    robot.keyPress(keyEMap(word));
                    robot.delay(1);
                    robot.keyRelease(keyEMap(word));
                }
                //调用map

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
